/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.grnt.buddy;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.grnt.buddy.BuddiesTreeControler.BuddiesCtlException;
import org.grnt.common.CommonFunc;
import org.grnt.dbcommon.ControlBean;
import static org.grnt.dbcommon.DataControler.TransOpt;

/**
 * It is subclass of ControlBean to implement delete a user action.
 * @author Sayid
 */
public class DelUser implements ControlBean {
	final Long   userId;
	final String sqlStr = "DELETE FROM gt_users WHERE user_id = '?' ";

	/**
	 *
	 * Constructure with one parameter user id.
	 * @param userId User id.
	 */
	public DelUser(Long userId) {
		this.userId = userId;
	}

	final public List<String> getCommands() {
		List<String> result = new ArrayList<String>();
		String command = CommonFunc.sqlStrFormat(sqlStr, userId.toString());
		command = CommonFunc.sqlNullStr2NullVal(command);
		result.add(command);
		return result;
	}

	final public boolean doInTransaction() {
		try {
			BuddiesTreeControler.delUser(userId);
		} catch (BuddiesCtlException ex) {
			Logger.getLogger(DelUser.class.getName()).log(Level.SEVERE, null, ex);
			BuddiesTreeControler.killUser(userId);
		}
		return true;
	}

	public void doAfterTransaction(TransOpt opt) {
		BuddiesTreeControler.killUser(userId);
	}
}
